﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.SystemManage
{
    public partial class HospitalInfo : BasePage
    {
        public HospitalInfo()
        {
            this.ModuleNo = Module.医疗机构管理.ToString();
            //this.PageOperateType = OperateType.编辑.ToString();
        }

        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_Area ia = Factory.BLL_Area();

        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定医疗机构基本信息
            if (!IsPostBack)
            {
                Model.HospitalInfo hospital = ih.GetHospitalInfoByID(CurrentAccount.GetUser().Org.OrgID);
                lblHospitalName.Text = hospital.HospitalName;
                txtContactPerson.Text = hospital.ContactPerson;
                txtContactTel.Text = hospital.TelNo;
                txtHospitalAddress.Text = hospital.Address;
                txtMobile.Text = hospital.CellNo;
                txtEmail.Text = hospital.Email;
                dplLevel.SelectedValue = hospital.Levels;
                hfdAreaId.Value = hospital.AreaID;
                lblArea.Text = GetFullAreaName(hospital.AreaID);

                #region 内蒙的

                dplLevelDetail.SelectedValue = hospital.LevelDetail;
                txtCode.Text = hospital.Code;
                ddlNature.SelectedValue = hospital.Nature;
                ddlEconomicType.SelectedValue = hospital.EconomicType;
                ddlOrganizers.SelectedValue = hospital.Organizers;
                ddlAffiliation.SelectedValue = hospital.Affiliation;
                txtBeds.Text = hospital.Beds;
                rblistCooperative.SelectedValue = hospital.IsCooperative ? "1" : "0";
                rblistInsurance.SelectedValue = hospital.IsInsurance ? "1" : "0";
                txtExpenditure.Text = hospital.Expenditure;
                //rblistOld.SelectedValue = hospital.IsOld ? "1" : "0";
                //绑定是否是县级以上
                radListHospitalCountyOver.SelectedIndex = hospital.CountyOver == true ? 0 : 1;

                #endregion 

                if (string.IsNullOrEmpty(hospital.TelNo))
                {
                    lblMsg.Visible = true;
                }
            }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtContactTel.Text.Trim()))      //update by lhl   2009-08-27
            {
                HZ.Web.WebUtil.Alert("电话号码不能为空！", this);
                txtContactTel.Focus();
                return;
            }

            //获取医疗机构信息
            Model.HospitalInfo hospitalInfo = new Model.HospitalInfo();
            hospitalInfo.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            hospitalInfo.HospitalName = CurrentAccount.GetUser().Org.OrgName;
            hospitalInfo.Address = txtHospitalAddress.Text.Trim();
            hospitalInfo.ContactPerson = txtContactPerson.Text.Trim();
            hospitalInfo.TelNo = txtContactTel.Text.Trim();
            hospitalInfo.CellNo = txtMobile.Text.Trim();
            hospitalInfo.Email = txtEmail.Text.Trim();
            hospitalInfo.Levels = this.dplLevel.SelectedValue;
            hospitalInfo.AreaID = hfdAreaId.Value;
            //基层或县级 
            hospitalInfo.CountyOver = radListHospitalCountyOver.SelectedIndex == 0 ? true : false;

            #region 内蒙的


            hospitalInfo.LevelDetail=dplLevelDetail.SelectedValue;
            hospitalInfo.Code=txtCode.Text;

            hospitalInfo.Nature=ddlNature.SelectedValue;
            hospitalInfo.EconomicType=ddlEconomicType.SelectedValue ;
            hospitalInfo.Organizers=ddlOrganizers.SelectedValue;
            hospitalInfo.Affiliation=ddlAffiliation.SelectedValue ;

            hospitalInfo.Beds=txtBeds.Text ;
            hospitalInfo.IsCooperative =rblistCooperative.SelectedValue=="1"? true : false;
            hospitalInfo.IsInsurance = rblistInsurance.SelectedValue == "1" ? true : false;
           hospitalInfo.Expenditure=txtExpenditure.Text ;
           //hospitalInfo.IsOld = rblistOld.SelectedValue == "1" ? true : false;

            #endregion
             
            Hospital hospital = new Hospital();
            hospital.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            hospital.HospitalName = CurrentAccount.GetUser().Org.OrgName;
            hospital.HospitalAddress = txtHospitalAddress.Text.Trim();
            hospital.ContactPerson = txtContactPerson.Text.Trim();
            hospital.ContactTel = txtContactTel.Text.Trim();
            //基层或县级 
            hospital.CountyOver = radListHospitalCountyOver.SelectedIndex == 0 ? true : false;

            //保存设置
            if (ih.UpdateHospitalInfo(hospitalInfo, hospital) == 1)
            {
                PageInfo("保存成功！", "");
            }
            else
            {
                PageError("保存失败！", "");
            }
        }

        /// <summary>
        /// 获取地区全路径
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        private string GetFullAreaName(string areaId)
        {
            string fullAreaName = "";
            IBLL_Area areaCtrl = Factory.BLL_Area();

            Area area = areaCtrl.GetArea(areaId);
            fullAreaName = area.AreaName;
            if (area.FatherAreaID != "0")
            {
                Area father = areaCtrl.GetArea(area.FatherAreaID);
                fullAreaName = father.AreaName + ">>" + fullAreaName;


                if (father.FatherAreaID != "0")
                {
                    Area father2 = areaCtrl.GetArea(area.FatherAreaID);
                    fullAreaName = father2.AreaName + ">>" + fullAreaName;
                }
            }
            return fullAreaName;
        }
    }
}
